<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>websocket</title>
</head>

<body>
  <h3>你好Ws</h3>

  <script>
    const client = new WebSocket('ws://127.0.0.1:3000')

    // 连接成功，触发，只执行一次  生命周期
    client.onopen = () => {
      console.log('连接 - 服务器 - 成功')
      // 向服务器发送消息
      client.send('我是张三')
    };

    // 接受服务器推送过来的数据 触发N多次
    client.onmessage = ({ data }) => {
      console.log(data)
    };

    client.onclose = () => {
      console.log('服务器离开了')
    }

  </script>
</body>

</html>